<?php
/**
 * Created by PhpStorm.
 * User: yuliang
 * Date: 2017/5/29
 * Time: 下午4:13
 */

namespace app\common;

use app\common;
use Flc\Alidayu\Client;
use Flc\Alidayu\App;
use Flc\Alidayu\Requests\AlibabaAliqinFcSmsNumSend;
use Flc\Alidayu\Requests\IRequest;
class SendMessage
{
    private $alidayu = [];

    public function __construct()
    {
        $this->alidayu = common\Config::$alidayu;
    }

    public function send($mobile,$code,$template = '')
    {
        $config = [
            'app_key'    => $this->alidayu['app_key'],
            'app_secret' => $this->alidayu['app_secret'],
//            'sandbox'    => $this->alidayu['sandbox'],  // 是否为沙箱环境，默认false
        ];
        $template = isset($template)?$template:$this->alidayu['template_code']; // 默认短信模版

        $client = new Client(new App($config));
        $req    = new AlibabaAliqinFcSmsNumSend;

        $req->setRecNum($mobile);
        $req->setSmsParam([
                'code' => $code
            ]);
        $req->setSmsFreeSignName($this->alidayu['sign_name']);
//        $req->setSmsTemplateCode($this->alidayu['template_code']);
        $req->setSmsTemplateCode($template);
//        $result = $client->execute($req);
        $resp = $client->execute($req);
//        if(!$result->result->success){
//            return false;
//        }else{
//            return true;
//        }

        if($resp->result->success){
            return true;
        }else{
            return false;
        }
    }

}